package scaladuct.validation

import scaladuct.{ConfigPageDecorator, ConfigResult}
import javax.validation.{ConstraintViolation, Validation, ValidatorFactory}

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 16.4.11
 * Time: 19:57
 * To change this template use File | Settings | File Templates.
 */

trait ValidatingPage[X] extends ConfigPageDecorator[X] {

  override def consume(cookie: Cookie): ConfigResult[X] = {
    val validatorFactory: ValidatorFactory = Validation.buildDefaultValidatorFactory();
    val validator: javax.validation.Validator = validatorFactory.getValidator();
    val constraintViolationSet: java.util.Set[ConstraintViolation[AnyRef]] = validator.validate(cookie);

    if (!constraintViolationSet.isEmpty) {
      throw new ConstraintViolationException(constraintViolationSet)
    }

    super.consume(cookie)
  }
}